<?php
namespace Api\Model;



class UserModel extends BaseModel {

    private static $instance;

    public static function singleton()
    {
        self::$instance === null and self::$instance = new self();
        return self::$instance;
    }

    public function getByOpenid($openid){
        $where['openid']=$openid;
        return $this->where($where)->find();
    }

    public function addUser($name,$hospital,$phone,$title,$duty,$openid){
        $data=array();
        $data['name']=$name;
        $data['hospital']=$hospital;
        $data['phone']=$phone;
        $data['title']=$title;
        $data['duty']=$duty;
        $data['openid']=$openid;
        $data['create_time']=date('Y-m-d H:i:s');
        return $this->add($data);
    }

    public function getById($user_id){
        $where['id']=$user_id;
        $row = $this->where($where)->find();
        return $row;
    }

    public function updateUser($name,$hospital,$phone,$title,$duty,$user_id){
        $data=array();
        $data['name']=$name;
        $data['hospital']=$hospital;
        $data['phone']=$phone;
        $data['title']=$title;
        $data['duty']=$duty;
        $res= $this->where(['id'=>$user_id])->save($data);
        return $res;
    }

    public function getUserArrByIds($user_ids)
    {
        $where['u.id']=['IN',$user_ids];
        $res = $this->alias('u')
            ->join('__WEIXIN_USER__ wu on u.openid=wu.openid')
            ->where($where)
            ->field('u.id,u.name,wu.avatarurl')
            ->select();
        $rows =[];
        foreach ($res as $key=>$val){
            $rows[$val['id']]=$val;
        }
        return $rows;
    }
}